package com.hzz.framework.scheduling;

import com.hzz.framework.annotation.NoAuthorize;
import com.hzz.framework.web.controller.BaseController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p> 动态任务变更
 */
@RestController
@Slf4j
@NoAuthorize
public class JobsSchedulingController extends BaseController {
    private static final String acPrefix = "/api/admin/";

    @Autowired
    JobsSchedulingConfigurer jobsSchedulingConfigurer;

    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}, value = acPrefix + "task/get")
    public Object taskGet() {
        log.info("taskGet.........");
        return jobsSchedulingConfigurer.get();
    }

    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}, value = acPrefix + "task/{op}/{jobName}")
    public Object taskUpdate(@PathVariable String op, @PathVariable String jobName, String cron) {
        log.info("taskUpdate.........");
        jobName = jobName.toLowerCase();
        switch (op.toLowerCase()) {
            case "update":
                return jobsSchedulingConfigurer.update(jobName, cron);
            case "cancel":
            case "delete":
            case "stop":
                return jobsSchedulingConfigurer.cancel(jobName);
            case "restart":
            case "reload":
                return jobsSchedulingConfigurer.restart(jobName);
            default:
                return jobsSchedulingConfigurer.get();
        }
    }
}
